package com.hooper.order.controller;

import com.hooper.hosp.enums.PaymentTypeEnum;
import com.hooper.hospital.common.result.Result;
import com.hooper.order.service.PaymentService;
import com.hooper.order.service.WeixinService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2022/10/11/21:35
 */
@RestController
@RequestMapping("/api/weixin")
public class WeixinController {

    @Resource
    private WeixinService weixinService;
    @Resource
    private PaymentService paymentService;

    @GetMapping("/create/{orderId}")
    public Result createNative(@PathVariable String orderId) {
        Map<String, Object> map = weixinService.createNative(orderId);
        return Result.ok(map);
    }

    @GetMapping("getPayStatus/{orderId}")
    public Result getPayStatus(@PathVariable String orderId){
        Map<String,String> result = weixinService.getPayStatus(orderId);
        if (result == null){
            return Result.fail().message("支付错误");
        }

        if ("SUCCESS".equals(result.get("trade_state"))){
            String outTradeNo = result.get("out_trade_no");
            paymentService.paySuccess(outTradeNo, PaymentTypeEnum.WEIXIN.getStatus(), result);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }
}
